/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */
// require("ocr.js")
// require("utils.js")
require("ggUtils快手直播.js")


var KSJSB_CURRENT_FLAG = '1'
var APP_NAME_KSJSB = '快手极速版'
var PACK_NAME_KSJSB = 'com.kuaishou.nebula' //'com.kuaishou.kgx.novel'
var MAIN_PAGE_FLAG_KSJSB = '首页'
var ENTER_CENTER_PAGE_FLAG_KSJSB = '去赚钱'
var CENTER_PAGE_FLAG_KSJSB = '我的金币'
var CURRENT_ERROR_FLAG = false

function skip_主界面的弹窗_KSJSB() {
    var iserror = false
    printinfoOld('skip_主界面的弹窗_KSJSB')
    //朋友推荐
    var detects = opencv.findImagesEx('图色260891.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    //找图返回目标数组
    var detects = opencv.findImagesEx('图色967291.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    if (ocrText('账号激活', '开始验证')) {
        agentOrHidback()
        sleep.second(3)
        iserror = true
        CURRENT_ERROR_FLAG = true
        if (ocrText('账号激活', '开始验证')) {
            agentOrHidback()
            sleep.second(3)
            iserror = true
            CURRENT_ERROR_FLAG = true
        }
    }

    agentOrHidClick(ocrText('我知道了', '确定', '取消', '忽略'))

    return iserror
}

function skip_任务中心的弹窗_KSJSB(isFirst) {
    if (isFirst && getExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG) <= 1) {
        var qdFlag = ocrWaitForMoreTextMillisecond('立即签到', '立即领取', 8)
        if (qdFlag) {
            agentOrHidClick(qdFlag)
            if (getExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG) > 1) {
                setExcuteFunc('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)
            }
        } else {
            //立即领取
            var detects = opencv.findImagesEx('图色410891.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
                setExcuteFunc('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)
            }
        }
    }

    //朋友推荐
    var detects = opencv.findImagesEx('图色260891.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    //找图返回目标数组
    var detects = opencv.findImagesEx('图色118333.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    if (ocrText('账号激活', '开始验证')) {
        agentOrHidback()
        sleep.second(3)
        CURRENT_ERROR_FLAG = true
    }

    //找图返回目标数组
    var detects继续 = opencv.findImagesEx('图色224353.cv');
    if (detects继续 != null) {
        agentOrHidClick(detects继续[0])
    }

    var ocr1close = opencv.OCREx('图色514102.cv');
    if (ocr1close != null && ocr1close.getAllString() == 'x') {
        agentOrHidClick(ocr1close.getAllDetect()[0])
    }

    agentOrHidClick(ocrWaitForMoreTextMillisecond('知道了', '跳过', '取消', '知道啦', '提醒我领取', '我知道', 4))
}

// 启动任务
function start_app_KSJSB() {
    printinfoOld('start_app_KSJSB')
    CURRENT_ERROR_FLAG = false

    if (!startApp(PACK_NAME_KSJSB)) {
        return false
    }

    // 跳过首页广告
    expectView(() => {
        // printinfoOld(ocrText('首页'))
        return ocrText(MAIN_PAGE_FLAG_KSJSB, '发现', '朋友')
    }, (times) => {
        skip_主界面的弹窗_KSJSB()

        if (times > 4) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        skip_主界面的弹窗_KSJSB()
        // agentOrHidClick(findView)
    }, null, 5)

    return true
}

function isIncenterPage_KSJB() {
    if (ocrText('任务中心', '点可领', '开宝箱', '我的现金', '日常任务', '去邀请', '现金')) {
        return true
    }

    return false
}

// 进入任务中心
function enter_任务中心_KSJSB(iskip) {
    printinfoOld('enter_任务中心_KSJSB')
    if (iskip == undefined) iskip = false

    expectView(() => {
        return ocrText(MAIN_PAGE_FLAG_KSJSB, '朋友', '生活')
    }, (times) => {
        skip_主界面的弹窗_KSJSB()

        if (times > 3) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
        // clickOffectY(7 / 10, 1, 60)
    }, null, 5)

    clickOffectY(7 / 10, 1, 60)

    if (!iskip) {
        // 期望标志位
        expectView(() => {
            return ocrText(CENTER_PAGE_FLAG_KSJSB)
        }, (times) => {
            skip_任务中心的弹窗_KSJSB(true)

            // if (times > 8) {
            //     agentOrHidback()
            //     sleep.millisecond(500)
            //     // todo 未找到后 返回怎么做
            // }
            sleep.millisecond(500)
        }, (findView) => {
            // agentOrHidClick(findView)
        }, null, 10)
    }

}


function open_签到_KSJSB() {
    log('签到')
    if (hasExcuteFunc('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)) {

        if (CURRENT_ERROR_FLAG == false && getExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG) < 2) {
            // ocrText('赚更多', true)
            watchKsGG_多次看广告(ocrTextMoreComplete('赚更多'))
        }
        return
    }

    if (getExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG) >= 3) {
        return
    }

    if (getExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG) > 1) {
        sleep.second(2)
        skip_任务中心的弹窗_KSJSB()
    }

    addExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)

    var qdFlag = ocrWaitForMoreTextMillisecond('立即签到', 8)
    if (qdFlag) {
        agentOrHidClick(qdFlag)
        if (getExcuteFuncTimes('open_签到_KSJSB' + KSJSB_CURRENT_FLAG) > 2) {
            setExcuteFunc('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)
        }
    } else {
        //立即领取
        var detects = opencv.findImagesEx('图色410891.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
            // setExcuteFunc('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)
            watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看广告'))
        }

    }

    agentOrHidClick(ocrText('开启提醒', '提醒我'), 2, () => {
        agentOrHidClick(ocrTextMoreComplete('始终允许', '允许'))
    })

    var qd_完成Flag = ocrTextMoreComplete('赚更多')
    if (qd_完成Flag) {
        // setExcuteFunc('open_签到_KSJSB' + KSJSB_CURRENT_FLAG)
        watchKsGG_多次看广告(qd_完成Flag)
    }
    watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看广告'))

    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '玩游戏送金币'))) {
        enter_任务中心_KSJSB(true)
    }

    skip_任务中心的弹窗_KSJSB()
}

function open_预约领金币_KSJSB() {
    if (getExcuteFuncTimes('open_预约领金币_KSJSB' + KSJSB_CURRENT_FLAG) > 4) {
        return
    }
    if (hasExcuteFunc('open_预约领金币_KSJSB_立即预约' + KSJSB_CURRENT_FLAG)) {
        return
    }
    log('预约领金币')
    move2Top(3, 300, '任务中心', '话费充值')
    skip_任务中心的弹窗_KSJSB()

    moveUp2FindWidTxtMoreByTimes(4, (findView) => {
        agentOrHidClick(findView)
        agentOrHidClick(ocrWaitForText('一键领取', 6), 1, () => {
            // setExcuteFunc('open_预约领金币_KSJSB_立即预约')
            agentOrHidClick(ocrWaitForText('开心收下'))
        })
        agentOrHidClick(ocrWaitForText('立即预约'), 1, () => {
            setExcuteFunc('open_预约领金币_KSJSB_立即预约' + KSJSB_CURRENT_FLAG)

        })
    }, '预约领金币', '今日预约', '预约金币', '一键领取')

    addExcuteFuncTimes('open_预约领金币_KSJSB' + KSJSB_CURRENT_FLAG)
    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '玩游戏送金币'))) {
        enter_任务中心_KSJSB(true)
    }

}

function open_开宝箱_KSJSB() {
    skip_任务中心的弹窗_KSJSB()
    log('开宝箱')
    var flag开宝箱 = ocrText('开宝箱得金币', '点就领', '点可领')
    if (flag开宝箱 == null) {
        //找色返回目标数组
        var detects = opencv.findMultiColorEx('图色612430.cv');
        if (detects != null) {
            flag开宝箱 = detects[0]
        }
    }


    agentOrHidClick(flag开宝箱, 1, () => {
        if (CURRENT_ERROR_FLAG == false) {
            // watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看视频', '看广告'))
            agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看视频', '看广告'))
            detail看快手广告()
        }

        skip_任务中心的弹窗_KSJSB()
    })
}


function go_点赞_KSJSB() {

    if (hasExcuteFunc('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)) {
        return
    }
    log('点赞')
    skip_任务中心的弹窗_KSJSB()

    move2Top(4, 300, '我的现金', '自动兑换', '任务中心')
    moveUp2FindWidTxtMoreByTimes(4, (findView) => {
        if (getExcuteFuncTimes('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG) > 1) {
            if (isInNearBy(findView, '已完成', 30)) {
                setExcuteFunc('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)
                return
            }
        }


        agentOrHidClick(findView)
        if (ocrText('点赞1个作品', '去点赞')) {
            if (getExcuteFuncTimes('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG) > 1) {
                setExcuteFunc('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)
            }
            return
        }

        ocrWaitForMoreTextMillisecond('分享', '精选', '首页', 3)

        var detectssp = opencv.findImagesEx('图色208716.cv');
        if (detectssp != null) {
            agentOrHidClick(detectssp[0])
        }


        //头像关注
        var detects1 = opencv.findImagesEx('图色971253.cv');
        if (detects1 != null) {
            agentOrHidClickOffectY(detects1[0], -140)
        }

        agentSwipBig()
        sleep.second(2)

        // 判断是否为红包界面
        var detectsflag = opencv.findImagesEx('图色362056.cv');
        if (detectsflag != null) {
            doubleClick(0.5, 0.5)
        } else {
            agentSwipBig()
        }


        // enter_任务中心_KS(true)
        // agentOrHidClick(ocrWaitForMoreTextMillisecond('知道了', '我知道', 6), 1, () => {
        //     setExcuteFunc('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)
        // })
        setExcuteFunc('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)

    }, '点赞1个', '去点赞')

    addExcuteFuncTimes('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)
    if (getExcuteFuncTimes('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG) >= 3) {
        setExcuteFunc('go_点赞_KSJSB' + KSJSB_CURRENT_FLAG)
    }

    // if (!backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '开宝箱'))) {
    //     enter_任务中心_KS(true)
    // }
    // backUntilFindView(() => ocrText('去赚钱', '抵用金', '日常任务'))
}



function open_每日打卡_KSJSB() {
    printinfoOld('open_每日打卡_KSJSB-------------------------------')
    if (hasExcuteFunc('open_每日打卡_KSJSB')) {
        return
    }
    skip_任务中心的弹窗_KSJSB()

    var flag每日打卡 = ocrText('每日打卡', '白拿好礼', '好礼')
    if (flag每日打卡 == null) {
        //找色返回目标数组
        var detects = opencv.findImagesEx('图色239200.cv');
        if (detects != null) {
            flag每日打卡 = detects[0]
        }
    }


    agentOrHidClick(flag每日打卡, 1, () => {
        ocrWaitForMoreTextMillisecond('今日打卡任务', '挑战进度', 6)
        agentOrHidClick(ocrText('去签到'), 1, () => {
            setExcuteFunc('open_每日打卡_KSJSB')
        })
    })

    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '玩游戏送金币'))) {
        enter_任务中心_KSJSB(true)
    }
}

function open_快影_KSJSB() {
    printinfoOld('open_快影_KSJSB-------------------------------')
    skip_任务中心的弹窗_KSJSB()

    if (hasExcuteFunc('open_快影_KSJSB' + KSJSB_CURRENT_FLAG)) {
        return
    }
    currentAppPackName = PACK_NAME_KSJSB
    move2Top(3, 300, '任务中心', '话费充值')
    moveUp2FindWidTxtMore((findView) => {
        agentOrHidClick(findView)
        // agentOrHidClick(ocrWaitForText('打开'))
        agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.7, 0.9, 1, 3, '始终允许', '允许', '打开'), 1, () => {
            agentOrHidClickAbsoultX(ocrTextMoreInRegion(0, 0.7, 1, 1, '取消', '打开'), screen.percentToWidth(0.75))
        })
        agentOrHidClick(ocrWaitForText('同意并进入'))
        setExcuteFunc('open_快影_KSJSB' + KSJSB_CURRENT_FLAG)
        sleep.second(1)
        // reStartApp()
    }, '打开快影')


    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领'))) {
        enter_任务中心_KSJSB(true)
    }
}


function watch_看广告_KSJSB() {
    printinfoOld('watch_看广告_KSJSB-------------------------------')

    if (CURRENT_ERROR_FLAG) {
        return
    }

    if (hasForverExcuteFuncInTime是否在一段时间内执行过('ks_sj_yc')) {
        logSleep('近期10金币异常')
        return
    }

    skip_任务中心的弹窗_KSJSB()

    if (hasExcuteFunc('watch_看广告_KSJSB' + KSJSB_CURRENT_FLAG)) {
        return
    }

    move2Top(5, 300, '任务中心', '话费充值')
    moveUp2FindWidTxtMore((findView) => {
        // watchKsGG_多次看广告(findView)
        agentOrHidClick(findView)
        detail看快手广告()
        // addExcuteFuncTimes('watch_看广告_KSJSB' + KSJSB_CURRENT_FLAG)
        setExcuteFunc('watch_看广告_KSJSB' + KSJSB_CURRENT_FLAG)
    }, '单日最高可得', '看广告得', '看视频得', '看广告')

    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '玩游戏送金币'))) {
        enter_任务中心_KSJSB(true)
    }
}

function go_吃饭_KSJSB() {
    printinfoOld('go_吃饭_KSJSB-------------------------------')
    skip_任务中心的弹窗_KSJSB()

    if (hasExcuteFunc('go_吃饭_KSJSB' + KSJSB_CURRENT_FLAG)) {
        return
    }

    move2Top(7, 300, '任务中心', '话费充值')
    moveUp2FindWidTxtMore((findView) => {
        agentOrHidClick(findView)
        agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.2, 1, 1, 4, '领取饭补', '领饭补'))
        // ocrText('看视频', true)

        var flag看视频 = ocrWaitForMoreTextMillisecondInRegion(0.1, 0.4, 1, 1, 6, '看广告', '看视频')
        if (flag看视频 == null) {

            var detects = opencv.findImagesEx('图色811630.cv');
            if (detects != null) {
                flag看视频 = detects[0]
            }
        }
        watchKsGG_多次看广告(flag看视频)
        // addExcuteFuncTimes('go_吃饭_KSJSB')
        if (ocrText('已完成')) {
            setExcuteFunc('go_吃饭_KSJSB' + KSJSB_CURRENT_FLAG)
        }
        agentOrHidClick(ocrText('开心收下'))
    }, '到饭点领')

    if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '玩游戏送金币'))) {
        enter_任务中心_KSJSB(true)
    }
}

function start_拍视频_KSJSB() {
    printinfoOld('start_拍视频_KSJSB-------------------------------')
    skip_任务中心的弹窗_KSJSB()

    if (hasExcuteFunc('start_拍视频_KSJSB' + KSJSB_CURRENT_FLAG)) {
        return
    }
    move2Top(7, 300, '任务中心', '话费充值')
    moveUp2FindWidTxtMore((findView) => {
        if (ocrText('完成1/1')) {
            setExcuteFunc('start_拍视频_KSJSB' + KSJSB_CURRENT_FLAG)
            return
        }
        agentOrHidClick(findView)
        agentOrHidClick(ocrWaitForMoreTextMillisecond('取消下载', 3))
        agentOrHidClick(ocrText('随手拍'), 2, () => {
            agentOrHidClick(ocrWaitForMoreTextMillisecond('取消下载', 3))
        })
        agentOrHidClick(ocrText('一键开启'), 1, () => {
            agentOrHidClick(ocrWaitForMoreTextMillisecond('取消下载', 2))
            agentOrHidClick(ocrText('确定'))
            agentOrHidClick(ocrWaitForMoreTextMillisecond('仅使用期间', 2))
            agentOrHidClick(ocrWaitForMoreTextMillisecond('仅使用期间', 2))
        })

        // ocrText('仅使用期间', true)
        agentOrHidClick(ocrWaitForMoreTextMillisecond('仅使用期间', 2))
        agentOrHidClick(ocrWaitForMoreTextMillisecond('仅使用期间', 2))
        agentOrHidClick(ocrText('取消下载'))

        //找图返回目标数组
        var detects = opencv.findImagesEx('图色849747.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
        }

        //开始拍摄
        printinfoOld('开始拍摄')
        var detects = opencv.findImagesEx('图色855134.cv');
        if (detects != null) {
            agentOrHidClick(detects[0], 0)
            agentOrHidClick(ocrText('一键开启'), 1, () => {
                agentOrHidClick(ocrWaitForMoreTextMillisecond('取消下载', 2))
                agentOrHidClick(ocrText('确定'))
                agentOrHidClick(ocrWaitForMoreTextMillisecond('仅使用期间', 2))
                agentOrHidClick(ocrWaitForMoreTextMillisecond('仅使用期间', 2))
            }), 2
        } else {
            click(0.5, 0.85)
            sleep.second(1)
        }

        agentOrHidClick(ocrWaitForMoreTextMillisecond('好的', 2), 1, () => {
            agentOrHidClick(ocrText('允许'))
            agentOrHidClickAbsoultX(ocrText('应用信息'), 20)
        })

        if (!ocrText('发瞬间')) {
            if (detects != null) {
                agentOrHidClick(detects[0])

            } else {
                click(0.5, 0.85)
                sleep.second(1)
            }
            agentOrHidClick(ocrText('下一步'))
        }


        printinfoOld('下一步')
        sleep.second(3)
        agentOrHidClickAbsoultX(ocrWaitForMoreTextMillisecond('发瞬间', 4), screen.percentToWidth(0.75))
        agentOrHidClick(ocrText('一步'))
        //找图返回目标数组
        var detects1 = opencv.findImagesEx('图色624225.cv');
        if (detects1 != null) {
            agentOrHidClick(detects1[0])
        }

        printinfoOld('发布')
        var fbFlag = ocrWaitForMoreTextMillisecond('发布', 3)
        if (!fbFlag) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色472967.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
            }
            // agentOrHidClickAbsoultX(ocrText('发瞬间'), screen.percentToWidth(0.75))
            fbFlag = ocrWaitForMoreTextMillisecond('发布', 3)
        }
        agentOrHidClick(fbFlag)

        agentOrHidClick(ocrWaitForText('忽略'))
        agentOrHidClick(ocrText('愉快收下'), 1, () => {
            setExcuteFunc('start_拍视频_KSJSB' + KSJSB_CURRENT_FLAG)
        })


        if (ocrText('朋友')) {
            setExcuteFunc('start_拍视频_KSJSB' + KSJSB_CURRENT_FLAG)
        }

        //找图返回目标数组
        var detects = opencv.findImagesEx('图色138170.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
        }

        agentSwipBig()
        sleep.second(1)
        detects = opencv.findImagesEx('图色138170.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
        }

        // printinfoOld('test')
        agentOrHidClick(ocrText('点击翻倍'))
        // addExcuteFuncTimes('go_吃饭_KSJSB')
    }, '拍视频最高', '发作品', '去发布')
}



// 进行浏览任务
// start_浏览任务_KSJSB()
function start_浏览任务_KSJSB(lltime) {
    if (lltime == undefined) lltime = 18
    start_app_KSJSB()

    skip_主界面的弹窗_KSJSB()
    if (CURRENT_ERROR_FLAG) {
        if (ocrText('账号激活', '开始验证')) {
            stopAllApp()
            start_app_KSJSB()
            skip_主界面的弹窗_KSJSB()
            CURRENT_ERROR_FLAG = true
        }
        return false
    }

    agentOrHidClick(ocrText('点击翻倍'))

    //找图返回目标数组
    var detectsclose = opencv.findImagesEx('图色507871.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

    if (ocrText('任务中心', '我的金币')) {
        agentOrHidback()
        sleep.second(1)
    }
    skip_主界面的弹窗_KSJSB()
    if (CURRENT_ERROR_FLAG) {
        return false
    }

    // if (!ocrText('直播', '了解', '咨询', '广告', '下载', '图文', '长图')) {
    //     // 节省流量
    //     press(0.5, 0.5)
    //     if (!ocrWaitForMoreTextMillisecond('分享给', '推荐给', '不感兴趣', 3)) {
    //         skip_主界面的弹窗_KSJSB()
    //     }
    //     if (!ocrWaitForMoreTextMillisecond('分享给', '推荐给', '不感兴趣', 3)) {
    //         backUntilFindView(() => ocrText('热门', '同城', '发现'), 2)
    //     }

    //     // ocrTextTrue('省流模式')
    //     moveUpSmaller2FindWid((findView) => {
    //         agentOrHidClickAbsoultX(findView, screen.getScreenWidth() - 200)
    //         click(0.5, 0.3)
    //     }, '省流模式', '模式', '省流')

    //     click(0.5, 0.3)
    // }

    agentSwipBig()

    if (!CURRENT_ERROR_FLAG) {

        startThredTask(() => {
            var detectsksjsb = opencv.findImagesEx('图色375512.cv');
            if (detectsksjsb != null) {
                agentOrHidClick(detectsksjsb[0])
            } else {
                var detects = opencv.findImagesEx('图色879298.cv');
                if (detects != null) {
                    agentOrHidClick(detects[0])
                }
                agentOrHidClickOffectY(ocrTextMoreComplete('快手板速版'), 100)
            }

            agentOrHidClick(ocrText('取消', '暂不开启', '暂不'))
        }, (stepTimes) => {
            log('心跳' + stepTimes)
            var allres = ocrAllRes()
            if (ocrTextMoreByRes(allres, '领取金币', '点赞', '收藏', '更多金币', '活跃', '广告商', '看广告')
                || ocrTextMoreByRes(allres, '图文', '长图', '金币助手', '开车', '入职', '聊天中', '古玩')
                || ocrTextMoreByRes(allres, '合集', '短剧', '剧场', '咨询', '法律', '穿搭', '世界杯', '工资', '老公')
                || ocrTextMoreByRes(allres, '妹妹', '美女', 'AI', 'Ai', '生成', '穿搭', '世界杯', '工资', '老公')) {
                log('包含文字图文法律AI类跳过')
                agentSwipBigForKS()
                sleep.second(1)
                return
            }

            if (ocrTextMoreByRes(allres, '开屏广告')) {
                log('包含文字开屏广告跳过')
                agentSwipBigForKS()
                sleep.second(1)
                return
            }

            var detects = opencv.findMultiColorEx('图色351576.cv');
            if (ocrTextMoreByRes(allres, '下单', '广告', '购买', '优选好物', '补肾', '购物', '播卖货', '直播卖') || detects != null) {
                log('检测购物/药广告/小黄车')
                //
                sleep.second(getRandomNum(20, 30))
                if (getRandomNum(0, 100) > 75) {
                    doubleClick(0.5, 0.5)
                }

                if (detects && Math.random() * 100 > 70) {
                    agentOrHidClick(detects[0], 5, () => {
                        agentOrHidClickOffectY(ocrText('关注'), -150)

                        var rountimes = getRandomNum(5, 10)
                        reperatAgentSwipSmallDown循环向下浏览_FOR_KS(false, rountimes)

                        backUntilFindView(() => {
                            // SystemPrint.log(isIn直播界面())
                            return ocrText('首页', '去赚', '同城')
                        }, 2, 3)
                    })

                }

                agentSwipBigForKS()
                sleep.second(1)
                return
            }

            // if (stepTimes % 3 == 0 || stepTimes % 4 == 0) {
            //     sleep.millisecond(Math.random() * 2000)
            //     // agentSwipDown(true)
            //     agentSwipBigForKS()
            //     sleep.millisecond(Math.random() * 1000 + 500)

            //     //找图返回目标数组
            //     var detects = opencv.findImagesEx('图色418085.cv');
            //     if (detects != null) {
            //         // agentSwipDown(true)
            //         agentSwipBigForKS()
            //     }
            // }

            if (stepTimes % 10 == 0) {
                agentOrHidClick(ocrTextMoreByRes(allres, '取消', '禁止', '暂不', '以后', '再说'))
            }

            if (stepTimes % 3 == 0 || stepTimes % 4 == 0) {
                log('随机向下滑动')
                agentSwipBigForKS()
                sleep.second(1)
            }

        }, lltime * 60)

    }
}



var nextJb = 0
function detail看快手广告(index) {
    log('开始看广告')
    sleep.second(1)
    if (hasForverExcuteFuncInTime是否在一段时间内执行过('detail看快手广告_low')) {
        log('10金币7天内不再执行')
        sleep.second(2)
        return
    }

    addExcuteFuncTimes('times_detail')
    if (!index) index = 0
    log('等待直到出现广告标识')
    var flag = ocrWaitForText('s后', '领取奖励', 10, 1)
    if (flag) {
        addExcuteFuncTimes('ksjsb_enter_ksp_error', 0)

        var maxNum = getMaxNumber(flag.text().slice(3))
        // SystemPrint.log('12sdfsdfsdfsdf'.slice(3))

        var ks最低金币 = parseInt(uiText.findByID(控件ID = 'ksjsb_zdjb').getText())
        if (((maxNum < ks最低金币 && maxNum > 60) || (nextJb < ks最低金币 && nextJb > 60))) {
            sleep.second(2)
            flag = ocrWaitForText('s后', '领取奖励', 3, 1)
            if (flag) {
                maxNum = getMaxNumber(flag.text().slice(3))
            }

            if (!isEnableSwitch('ksjsb_wxz')) {
                if (((maxNum < ks最低金币 && maxNum > 60) || (nextJb < ks最低金币 && nextJb > 60) && !ocrText('直播'))) {
                    log('低于' + ks最低金币 + '终止')
                    agentOrHidback()
                    sleep.second(3)
                    agentOrHidClick(ocrText('放弃奖励', '坚持退出', '退出', '放弃'))

                    //找图返回目标数组
                    var detectsclose = opencv.findImagesEx('图色404051.cv');
                    if (detectsclose != null) {
                        agentOrHidClick(detectsclose[0])
                    }
                    addExcuteFuncTimes('ksjsb_lowjb_times')
                    // isLowerJb = true
                    return
                }
            }

        }

        if ((maxNum < 2500 && maxNum > 2000)
            || (maxNum < 2000 && maxNum > 1600)
            || (maxNum < 1600 && maxNum > ks最低金币)
            || (maxNum < ks最低金币 && maxNum > 50)
            || (nextJb < ks最低金币 && nextJb > 50)) {
            log('低金币看直播')
            addExcuteFuncTimes('ksjsb_low_times')
        }

        if ((maxNum > 1200)) {
            addExcuteFuncTimes('ksjsb_lowjb_times', 0)
        }

        if (maxNum >= 2500 || maxNum == 1000 || maxNum == 1600 || maxNum == 2000 || maxNum == 1666 || maxNum == 770) {
            log('高额金币')
            addExcuteFuncTimes('ksjsb_lowjb_times', 0)
            addExcuteFuncTimes('ksjsb_low_times', 0)
        }



        if (maxNum == 10 && ocrText('10金币')) {
            log('10金币异常??')
            agentOrHidback()
            sleep.second(3)
            agentOrHidClick(ocrText('放弃奖励', '坚持退出', '退出', '放弃'))
            setExcuteFunc('detail看快手广告_low')
            setForverExcuteFuncInTime一段时间内不再执行('detail看快手广告_low', 7 * 24 * 60 * 60)
            isLowerJb = true
            return
        }

        if ((maxNum > 0 && maxNum < 50)) {
            sleep.second(2)
            flag = ocrWaitForText('s后', '领取奖励', 3, 1)
            if (flag) {
                maxNum = getMaxNumber(flag.text().slice(3))
                // maxNum = getMaxNumber('1s后领取观看奖励 ×'.slice(3))
            }

            if ((maxNum > 9 && maxNum < 50)) {
                log('当前金币' + maxNum + ',低于50换一个')
                agentOrHidback()
                sleep.second(3)

                agentOrHidClick(ocrText('放弃奖励', '坚持退出', '退出', '放弃'))
                var detectsclose = opencv.findImagesEx('图色404051.cv');
                if (detectsclose != null) {
                    agentOrHidClick(detectsclose[0])
                }
                addExcuteFuncTimes('ksjsb_lowjb_times')
                // return
            }
        }

    } else {
        // 直播广
        log('直播广??')
        if (ocrText('更多直播', '关注', '抽大奖', '小时榜', '说点什么', '送礼得金币', '游戏频道')) {
            addExcuteFuncTimes('ksjsb_enter_ksp_error', 0)
            sleep.second(3)
            log('关闭中间弹窗')
            var detectsclose = opencv.findImagesEx('图色813297.cv');
            if (detectsclose != null) {
                agentOrHidClick(detectsclose[0])
            }
            sleep.second(15)
            ocrWaitForText('已领取', 15, 4)
            //找图返回目标数组
            var detectsclose = opencv.findImagesEx('图色866412.cv');
            if (detectsclose != null) {
                agentOrHidClick(detectsclose[0])
            }
            agentOrHidback()
            sleep.second(2)
            var falgmore = ocrTextMoreComplete('领取奖励')
            if (falgmore) {
                agentOrHidClick(falgmore, 2, () => {
                    detail看快手广告()
                })
            } else {
                agentOrHidClick(ocrText('退出直播间', '退出直播', '出直播'))
            }


            return
        } else {
            if (ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '任务中心')) {
                logSleep('并未进入广告直接退出')
                addExcuteFuncTimes('ksjsb_enter_ksp_error')
                return
            }
        }
    }


    // ocrTextTrue('已成功领取')
    if (!ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '任务中心')) {
        log('获取广告时长')
        var sleeptime = getDectNumber(ocrWaitForText('s后', 's', '领取奖励', 5, 2))
        if (sleeptime < 5 || sleeptime > 30) {
            sleeptime = 5
        }
        logSleepTime('获取广告时长' + sleeptime, sleeptime)

        let changetimes = 0
        if (getRandomNum(0, 100) > 60 || maxNum == 0 || nextJb < 1600 || getExcuteFuncTimes('ksjsb_low_times') > 0) {
            log('等待完播')
            var rgbs
            whilestep((stepTimes, alltimes, canBreak) => {
                rgbs = get界面采样点()
                var allres = ocrAllRes()

                agentOrHidClick(ocrTextMoreByRes(allres, '放弃福利'))
                if (ocrTextMoreByRes(allres, '恭喜获得奖励', '已成功', '成功领取', '功领取')) {
                    canBreak()
                }
            }, 60, 2, (canBreak) => {
                if (check界面是否不再变化(rgbs)) {
                    if (!ocrText('s后', '秒后', 's', '秒', '恭喜获得奖励')) {
                        changetimes = changetimes + 1
                        log('界面不再变化!!!')
                        if (changetimes >= 2) {
                            canBreak()
                        }
                    }
                }
            })
        }

        if (!isEnableSwitch('ksjsb_wxz')) {
            if (getExcuteFuncTimes('ksjsb_low_times') > 0
                || (nextJb > 500 && nextJb < 1600)
                || (nextJb > 2000 && nextJb < 2500)
                || getExcuteFuncTimes('times_detail') % (maxNum == 0 ? 3 : 6) == 0) {
                agentOrHidClick(ocrWaitForText('直播中', '直播', '播中', '点击进入', 3, 1), 2, () => {
                    sleep.second(5)
                    agentOrHidClick(ocrText('立即关注'))
                    log('关闭中间弹窗')
                    var detectsclose = opencv.findImagesEx('图色813297.cv');
                    if (detectsclose != null) {
                        agentOrHidClick(detectsclose[0])
                    }

                    sleep.second(20)
                    log('关闭中间弹窗')
                    var detectsclose = opencv.findImagesEx('图色813297.cv');
                    if (detectsclose != null) {
                        agentOrHidClick(detectsclose[0])
                    }

                    if (getRandomNum(0, 100) > 50) {
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('go看直播挂机_点赞')) {
                            var fordzTimes = getRandomNum(30, 80)
                            for (var i = 0; i < fordzTimes; i++) {
                                click(0.5, 0.4, 50)
                                click(0.5, 0.4, 300)
                            }
                            setExcuteFuncInTime一段时间内不再执行('go看直播挂机_点赞', 5 * 60)
                        }
                        sleep.second(5)
                    }

                    log('打开橱窗或小黄车')
                    var flag = ocrText('讲解')
                    if (flag == null
                        || hasExcuteFuncInTime是否在一段时间内执行过('ksjsb_ccyc')
                        || hasExcuteFuncInTime是否在一段时间内执行过('ksjsb_zjgm')) {
                        //找图返回目标数组
                        var detectscc = opencv.findImagesEx('图色915187.cv');
                        if (detectscc != null) {
                            flag = detectscc[0]
                        }
                    } else {
                        setExcuteFuncInTime一段时间内不再执行('ksjsb_zjgm', 5 * 60)
                    }
                    agentOrHidClick(flag)
                    if (ocrText('当前操作', '请稍', '再试')) {
                        agentOrHidback()
                        sleep.second(2)
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('go看直播挂机_点赞')) {
                            var fordzTimes = getRandomNum(26, 50)
                            for (var i = 0; i < fordzTimes; i++) {
                                click(0.5, 0.4, 50)
                                click(0.5, 0.4, 300)
                            }
                            setExcuteFuncInTime一段时间内不再执行('go看直播挂机_点赞', 5 * 60)
                        }
                        setExcuteFuncInTime一段时间内不再执行('ksjsb_ccyc', 20 * 60)
                    }
                    sleep.second(20)



                    backUntilFindView(() => {
                        return ocrText('已成功', '成功领取', '已成', '功领取', 's后', '后可', '领取奖励')
                    }, 5, 2, () => {
                        agentOrHidClick(ocrText('退出直播间', '退出并', '出并关注', '退出直播', '坚持', '退出'))
                    })

                    addExcuteFuncTimes('ksjsb_low_times', 0)
                })
            }

        }

        log('等待获取已成功领取')
        var closeflag = ocrWaitForText('已成功领取', '已成功', '成功领取', '已成', '功领取', 10, 3)
        if (closeflag == null) {
            log('检测关闭标识')
            var detectsclose = opencv.findImagesEx('图色259307.cv');
            if (detectsclose != null) {
                closeflag = detectsclose[0]
            }
        }

        if (closeflag == null) {
            if (ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '任务中心')) {
                log('已经再主页面')
                return
            }

            if (ocrText('领取奖励', 's后')) {
                log('再次检测结束广告标识')
                closeflag = ocrWaitForText('已成功领取', '已成功', '成功领取', '已成', '功领取', 5, 3)
            }
        }

        if (closeflag) {
            log('床有70概率完播')
            var allres = ocrAllRes()
            // '精华', '高端', '皮肤', '精华', '香水'
            if (ocrTextMoreByRes(allres, '床', '沙发', '家具') && getRandomNum(0, 100) > 30) {
                // 等待完播
                //找图返回目标数组

                log('等待完播最长60秒')
                var detail = expectViewSample(() => {
                    var detects = opencv.findImagesEx('图色487864.cv');
                    if (detects != null) {
                        return detects[0]
                    }
                    return null
                }, null, null, null, 20, 3)

                // 去看详情
                // agentOrHidClick(detail, 2, () => {
                //     watch_ks_直播()
                // })
            }
        }

        agentOrHidClick(closeflag, 1)

        if (ocrWaitForMoreTextMillisecond('下单多得', '领取额外', 4)) {
            // ocrTextTrue('额外金币')
            log('额外金币')
            agentOrHidClick(ocrWaitForMoreTextMillisecond('领取额外', '额外金币'))
            sleep.second(3)
            backUntilFindView(() => {
                var allres = ocrAllRes()
                if (ocrTextMoreByRes(allres, '已成功', '成功领取', '已成', '功领取', 's后', '后可', '领取奖励', '看广告得', '现金:')) {
                    return true
                }
                if (ocrTextMoreByRes(allres, '金币:', '添加好友', '瓜分百亿', '看指定视频', '点可领')) {
                    return true
                }
                return false
            }, 5, 2, () => {
                agentOrHidClick(ocrText('退出直播间', '退出并', '出并关注', '退出直播', '坚持', '退出'))
            })

            agentOrHidClick(closeflag)
        }
    }

    log('再看一个')
    if (ocrWaitForText('再看一个', '看一个', '再领', '再得', '再看1')) {
        nextJb = getDectNumber(ocrTextMoreInCenter('额外', '最高'))
        agentOrHidClick(ocrTextMoreComplete('领取奖励'), 2, () => {
            detail看快手广告()
        })
    } else {
        log('未检查到再看一个')
        if (!ocrText('任务中心', '点就领', '抵用金', '日常任务', '金币:', '点可领')) {
            if (ocrText('本次有额外', '额外金币', '下载并', '并体验', '打开并')) {
                log('检测到下载任务不做')
                var detectsclose = opencv.findImagesEx('图色853995.cv');
                if (detectsclose != null) {
                    agentOrHidClick(detectsclose[0])
                }
            } else {
                agentOrHidback()
                sleep.second(1)
            }

            var flag = ocrWaitForText('换一个', '一个', '换一')
            if (flag == null) {
                flag = ocrTextMoreComplete('领取奖励')
            }
            if (flag == null) {
                flag = ocrText('继续观看', '继续')
            }
            agentOrHidClick(flag, 1, () => {
                detail看快手广告()
            })
        }

    }

    log('结束单次广告')
}


var isLowerJb = false
function start_疯狂看广告_KSJSB() {
    if (!start_app_KSJSB()) {
        return
    }
    enter_任务中心_KSJSB(true)
    skip_任务中心的弹窗_KSJSB()

    isLowerJb = false
    log('倒计时6秒进行匹配看广告')
    ocrWaitForText('任务中心', '日常任务', '抵用金', '点就领', '点可领', '看广告得', 3, 2)
    log('查找看广告')
    if (!ocrText('单日最高可得', '看广告得', '看视频得',)) {
        moveUp2FindWidTxtMore((findView) => {
            agentOrHidClick(findView)
            detail看快手广告()
        }, '单日最高可得', '看广告得', '看视频得')

        move2Top(4, 300, '任务中心', '话费充值')
        skip_任务中心的弹窗_KSJSB()
    }


    while (true) {
        if (isEnableSwitch('ks_fkkbx')) {
            var flag开宝箱 = null
            var detects = opencv.findMultiColorEx('图色612430.cv');
            if (detects != null) {
                flag开宝箱 = detects[0]
            }
            if (flag开宝箱 == null) {
                flag开宝箱 = ocrText('开宝箱得金币', '点就领', '点可领')
            }
            agentOrHidClick(flag开宝箱, 1)
            agentOrHidClick(ocrWaitForText('去看广告', '看广告得', '看视频最高'))
            detail看快手广告()
        }

        agentOrHidClick(ocrText('单日最高可得', '看广告得', '看视频得', '看视频最高'), 2, () => {
            detail看快手广告()
        })

        if (getExcuteFuncTimes('ksjsb_enter_ksp_error') > 1) {
            logSleep('多次未成功进入广告')

            move2Top(2)
            moveUp2FindWidTxtMore((findView) => {
                agentOrHidClick(findView)
                detail看快手广告()
            }, '单日最高可得', '450万', '看广告得', '看视频得')
        }
        if (getExcuteFuncTimes('ksjsb_lowjb_times') > 2) {
            isLowerJb = true
        }

        if (isLowerJb) {
            logSleep('低金币自动停止并浏览', 60)
            stopAllApp()

            start_浏览任务_KSJSB(10)
        }

        if (isEndTimeLeft5Min()) {
            logSleep('晚上23:55后自动暂停', 300)
        }

        if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领', '点可领', '到饭点', '任务中心'))) {
            enter_任务中心_KSJSB(true)

            skip_任务中心的弹窗_KSJSB()
            if (!hasExcuteFunc('ksjsb_疯狂看广告')) {
                move2Top(4, 300, '任务中心', '话费充值')
            }
            moveUp2FindWidTxtMore((findView) => {
                if (ocrText('倒计时结束')) {
                    setExcuteFunc('ksjsb_疯狂看广告')
                }

                if (isEnableSwitch('ks_fkkbx')) {
                    var flag开宝箱 = null
                    var detects = opencv.findMultiColorEx('图色612430.cv');
                    if (detects != null) {
                        flag开宝箱 = detects[0]
                    }
                    if (flag开宝箱 == null) {
                        flag开宝箱 = ocrText('开宝箱得金币', '点就领', '点可领')
                    }
                    agentOrHidClick(flag开宝箱, 1)
                    agentOrHidClick(ocrWaitForText('去看广告', '看广告得', '看视频最高'))
                    detail看快手广告()
                } else {
                    agentOrHidClick(findView, 2, () => {
                        detail看快手广告()
                    })
                }
            }, '单日最高可得', '450万', '看广告得', '看视频得', '看视频最高')
        }
    }
}

function go_搜索药片_KSJSB() {
    if (!start_app_KSJSB()) {
        return
    }

    skip_主界面的弹窗_KSJSB()

    if (CURRENT_ERROR_FLAG) {
        return
    }

    log('检测搜素')
    expectView(() => {
        var detects = opencv.findImagesEx('图色465095.cv');
        if (detects != null) {
            return detects[0]
        }
        return null
    }, () => {
        agentSwipBig()
        sleep.second(5)
    }, (findView) => {
        agentOrHidClick(findView)
    }, () => {
        clickAbs(screen.percentToWidth(1) - 80, 150)
    }, 8)

    if (ocrWaitForText('历史', '猜你', '搜索发现', '我的热点')) {
        Clipboard.copy('诺特兰德烟酰胺')
        // Clipboard.copy('碧迪')
        sleep.second(2)
        var fallg = ocrTextMoreInRegion(0, 0.5, 1, 1, '诺特兰', '烟酰胺', '兰德烟')
        if (fallg == null) {
            fallg = ocrText('诺特兰', '兰德烟')
        }
        agentOrHidClick(fallg, 3)

        var flagsearch = ocrTextInRegion(0, 0, 1, 0.2, '搜索')
        if (!flagsearch) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色848962.cv');
            if (detects != null) {
                flagsearch = detects[0]
            }
        }
        agentClickRight(flagsearch)

        ocrWaitForText('直播卖货', 3, 2)

        repeatFindView(() => {
            //找图返回目标数组
            return ocrText('直播卖货', '直播')
        }, (findView) => {
            //找图返回目标数组
            agentOrHidClick(findView)
            // 挂会儿直播
            watch_ks_直播(() => {
                agentOrHidClick(ocrText('关注'))
            })
        })

    }


    stopAllApp()
}


function start_收集金币_KSJSB() {
    if (getExcuteFuncTimes('start_收集金币_KSJSB') >= 6) {
        return
    }

    // if (hasExcuteFunc('start_收集金币_KSJSB')) {
    //     return
    // }

    if (!start_app_KSJSB()) {
        return
    }

    currentAppPackName = PACK_NAME_KSJSB

    addExcuteFuncTimes('start_收集金币_KSJSB')

    startThredTaskMaxTime超时结束(() => {
        enter_任务中心_KSJSB()

        open_签到_KSJSB()

        open_预约领金币_KSJSB()

        if (!isIncenterPage_KSJB()) {
            enter_任务中心_KSJSB(true)
        }

        open_开宝箱_KSJSB()

        watch_看广告_KSJSB()

        // if (CURRENT_ERROR_FLAG == false && !hasExcuteFunc('detail看快手广告_low')) {
        //     // watch_看广告_KSJSB()


        //     if (!isIncenterPage_KSJB()) {
        //         enter_任务中心_KSJSB(true)
        //     }
        //     // go_吃饭_KSJSB()

        //     move2Top(3)
        // }

        go_点赞_KSJSB()

        agentOrHidClick(ocrText('领取奖励', '领取金币', '立即领取', '去开启', '立即签到', '待领'), 2, () => {
            skip_任务中心的弹窗_KSJSB()

            agentOrHidClick(ocrText('立即领取', '领取金币', '领取奖励', '去开启', '立即签到', '待领'))
            agentSwipSmallDown()
            agentOrHidClick(ocrText('立即领取', '领取奖励', '领取金币', '去开启', '立即签到', '待领'))
        })
    })



    // open_快影_KSJSB()

    // start_拍视频_KSJSB()

    // closeApp(PACK_NAME_KSJSB)

    stopAllApp()
}


